odoo.define('web_ps_report.PsReportModel', function (require) {
    'use strict';

    var BasicModel = require('web.BasicModel');

    var PsReportModel = BasicModel.extend({
        //--------------------------------------------------------------------------
        // Public
        //--------------------------------------------------------------------------

        /**
         * @override
         * @returns {Object}
         */
        get: function (id, options) {
            var res = this._super.apply(this, arguments);
            if (res) {
                res.psdata = this.psdata;
            }
            return res;
        },
        /**
         * @override
         * @param {Object} params
         * @param {string} params.modelName
         * @param {string} params.dateStart
         * @param {string} params.dateStop
         * @param {string} params.measure
         * @param {string} params.interval
         * @param {Array[]} params.domain
         * @param {string} params.mode
         * @param {string} params.timeline
         * @param {string} params.render_type
         * @returns {Deferred}
         */
        load: function (params) {
            this._super.apply(this, arguments);
            this.modelName = params.modelName;
            this.measure = params.measure;
            this.interval = params.interval;
            this.domain = params.domain;
            this.ps_fields = params.ps_fields;
            this.timeRange = params.timeRange || [];
            this.comparisonTimeRange = params.comparisonTimeRange || [];
            this.compare = params.compare;
            this.mode = params.mode;
            this.timeline = params.timeline;
            this.ps_fields = params.ps_fields;
            this.render_type = params.render_type;
            this.fields = params.fields;
            this.fieldsInfo = params.fieldsInfo;
            this.psdata = {
                measure: this.measure,
                interval: this.interval,
                fields: this.fields,
                fieldsInfo: this.fieldsInfo,
            };
            this.context = params.context;
            return this._fetchData();
        },
        /**
         * Reload data.
         *
         * @param {any} handle
         * @param {Object} params
         * @param {string} params.measure
         * @param {string} params.interval
         * @param {Array[]} params.domain
         * @param {Object} params.timeRangeMenuData
         * @returns {Deferred}
         */
        reload: function (handle, params) {
            if (params.context !== undefined) {
                var timeRangeMenuData = params.context.timeRangeMenuData;
                if (timeRangeMenuData) {
                    this.timeRange = timeRangeMenuData.timeRange || [];
                    this.comparisonTimeRange = timeRangeMenuData.comparisonTimeRange || [];
                    this.compare = this.comparisonTimeRange.length > 0;
                } else {
                    this.timeRange = [];
                    this.comparisonTimeRange = [];
                    this.compare = false;
                }
            }
            if ('measure' in params) {
                this.data.measure = params.measure;
            }
            if ('interval' in params) {
                this.data.interval = params.interval;
            }
            if ('domain' in params) {
                this.domain = params.domain;
            }
            this.render_type = params.render_type;

            return this._fetchData();
        },

        //--------------------------------------------------------------------------
        // Private
        //--------------------------------------------------------------------------

        /**
         * Fetch ps_report data.
         *
         * @private
         * @returns {Deferred}
         */

        set_search_domian: function () {
            var domain_fields = {};
            for (var i = 0; i < this.ps_fields.length; i++) {
                var k_field = this.ps_fields[i].attrs.name;
                if (this.fields[k_field].formatType == "many2one") {
                    if (this.fields[k_field].value) {
                        domain_fields[k_field] = this.fields[k_field].value.res_id;
                    }
                } else if (this.fields[k_field].formatType == "many2many") {
                    if (this.fields[k_field].value.res_ids.length > 0) {
                        domain_fields[k_field] = this.fields[k_field].value.res_ids;
                    }
                } else if (this.fields[k_field].type == "date") {
                    domain_fields[k_field] = $("input[name='" + k_field + "']").val()
                } else if (this.fields[k_field].formatType == "boolean") {
                    if (this.fields[k_field].value) {
                        domain_fields[k_field] = this.fields[k_field].value;
                    } else {
                        domain_fields[k_field] = false;
                    }
                } else {
                    domain_fields[k_field] = $('.' + k_field).val()
                }
            }
            return domain_fields
        },


        _fetchData: function () {
            var self = this;
            var render_type = $('#render_type').val() || 'table';
            var defs = [
                this._rpc({
                    model: this.modelName,
                    method: 'get_report_data',
                    kwargs: {
                        render_type: this.render_type || render_type,
                        domain_fields: this.set_search_domian(),
                        context_fields: this.context.context_fields
                    }
                })];
            if (this.compare) {
                defs.push(this._rpc({
                        model: this.modelName,
                        method: 'get_report_data',
                        kwargs: {
                            render_type: this.render_type,
                            domain_fields: {},

                        }
                    })
                );
            }
            return $.when.apply($, defs).then(function () {
                var results = Array.prototype.slice.call(arguments);
                self.psdata.report = results[0];
                if (self.compare) {
                    self.psdata.comparisonReport = results[1];
                } else {
                    self.psdata.comparisonReport = undefined;
                }
                self.trigger_up('need_render');
            });
        },
    });

    return PsReportModel;

});
